package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.CldContract;
import com.cleaningcloud.cld.domain.CldDegreetitle;
import com.cleaningcloud.cld.domain.CldDegreetitleProject;
import com.cleaningcloud.cld.domain.CldDegreetitleResult;
import com.cleaningcloud.cld.domain.dto.CldDegreetitleDto;
import com.cleaningcloud.cld.domain.vo.CldDegreetitleProblemVo;
import com.cleaningcloud.cld.domain.vo.CldDegreetitleVo;
import com.cleaningcloud.cld.mapper.CldContractMapper;
import com.cleaningcloud.cld.mapper.CldDegreetitleMapper;
import com.cleaningcloud.cld.mapper.CldDegreetitleProjectMapper;
import com.cleaningcloud.cld.mapper.CldDegreetitleResultMapper;
import com.cleaningcloud.cld.service.ICldDegreetitleService;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 员工满意度主题Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-10-27
 */
@Service
public class CldDegreetitleServiceImpl implements ICldDegreetitleService {
    @Autowired
    private CldDegreetitleMapper cldDegreetitleMapper;

    @Autowired
    private CldDegreetitleResultMapper cldDegreetitleResultMapper;

    @Autowired
    private CldDegreetitleProjectMapper degreetitleProjectMapper;

    @Autowired
    private CldContractMapper cldContractMapper;

    /**
     * 查询员工满意度主题
     *
     * @param degreetitleId 员工满意度主题主键
     * @return 员工满意度主题
     */
    @Override
    public CldDegreetitle selectCldDegreetitleByDegreetitleId(Long degreetitleId) {
        return cldDegreetitleMapper.selectCldDegreetitleByDegreetitleId(degreetitleId);
    }

    /**
     * 查询员工满意度主题列表
     *
     * @param cldDegreetitle 员工满意度主题
     * @return 员工满意度主题
     */
    @Override
    public List<CldDegreetitleVo> selectCldDegreetitleList(CldDegreetitleDto cldDegreetitle) {
        return cldDegreetitleMapper.selectCldDegreetitleList(cldDegreetitle);
    }

    /**
     * 新增员工满意度主题
     *
     * @param cldDegreetitle 员工满意度主题
     * @return 结果
     */
    @Override
    public int insertCldDegreetitle(CldDegreetitle cldDegreetitle) {
        cldDegreetitle.setCreateTime(DateUtils.getNowDate());
        cldDegreetitle.setCreateId(SecurityUtils.getUserId());
        cldDegreetitle.setCreateName(SecurityUtils.getUsername());
        cldDegreetitle.setSendStatus(0L);
        cldDegreetitle.setFlag(0L);
        return cldDegreetitleMapper.insertCldDegreetitle(cldDegreetitle);
    }

    /**
     * 修改员工满意度主题
     *
     * @param cldDegreetitle 员工满意度主题
     * @return 结果
     */
    @Override
    public int updateCldDegreetitle(CldDegreetitle cldDegreetitle) {
        return cldDegreetitleMapper.updateCldDegreetitle(cldDegreetitle);
    }

    @Override
    @Transactional
    public int startSurvey(CldDegreetitleDto cldDegreetitle) {
        // 批量删除
        degreetitleProjectMapper.deleteCldDegreetitleProjectByDegreetitleId(cldDegreetitle.getDegreetitleId());
        // 类型为全选时
        if (cldDegreetitle.getProjectType() == 1) {
            // 查询签订中的项目
            CldContract query = new CldContract();
            query.setContractStatus(1L);
            List<CldContract> cldContractList = cldContractMapper.selectCldContractList(query);
            List<CldDegreetitleProject> projectList = new ArrayList<>();
            for (CldContract contract : cldContractList) {
                CldDegreetitleProject degreetitleProject = new CldDegreetitleProject();
                degreetitleProject.setDegreetitleId(cldDegreetitle.getDegreetitleId());
                degreetitleProject.setProjectId(contract.getProjectId());
                projectList.add(degreetitleProject);
            }
            cldDegreetitle.setProjectList(projectList);
        }
        degreetitleProjectMapper.batchCldDegreetitleProject(cldDegreetitle.getProjectList());
        cldDegreetitle.setSendStatus(1L);
        cldDegreetitle.setSendTime(DateUtils.getNowDate());
        return cldDegreetitleMapper.updateCldDegreetitle(cldDegreetitle);
    }

    /**
     * 批量删除员工满意度主题
     *
     * @param degreetitleIds 需要删除的员工满意度主题主键
     * @return 结果
     */
    @Override
    public int deleteCldDegreetitleByDegreetitleIds(Long[] degreetitleIds) {
        return cldDegreetitleMapper.deleteCldDegreetitleByDegreetitleIds(degreetitleIds);
    }

    /**
     * 删除员工满意度主题信息
     *
     * @param degreetitleId 员工满意度主题主键
     * @return 结果
     */
    @Override
    public int deleteCldDegreetitleByDegreetitleId(Long degreetitleId) {
        return cldDegreetitleMapper.deleteCldDegreetitleByDegreetitleId(degreetitleId);
    }

    @Override
    public CldDegreetitleVo selectSurveryDetails(CldDegreetitleDto cldDegreetitleDto) {
        CldDegreetitleVo cldDegreetitleVo = cldDegreetitleMapper.selectSurveryDetails(cldDegreetitleDto);
        // 查询答案
        if (cldDegreetitleVo != null) {
            for (CldDegreetitleProblemVo cldDegreetitleProblemVo : cldDegreetitleVo.getProblemList()) {
                CldDegreetitleResult query = new CldDegreetitleResult();
                query.setPersonId(cldDegreetitleDto.getPersonId() == null ? SecurityUtils.getUserId() : cldDegreetitleDto.getPersonId());
                query.setProjectId(cldDegreetitleDto.getProjectId());
                query.setDegreetitleProblemId(cldDegreetitleProblemVo.getDegreetitleProblemId());
                query.setDegreetitleId(cldDegreetitleVo.getDegreetitleId());
                cldDegreetitleProblemVo.setResult(cldDegreetitleResultMapper.selectCldDegreetitleResult(query));
            }
        }
        return cldDegreetitleVo;
    }
}
